package car

import (
	"context"
	"fmt"
	carpb "serve/car/api/gen/v1"
	rentalpb "serve/rental/api/gen/v1"
	"serve/shared/id"
)

type Manager struct {
	CarService carpb.CarServiceClient
}

func (m *Manager) Verify(cxt context.Context, carId id.CarID, location *rentalpb.Location) error {
	car, err := m.CarService.GetCar(cxt, &carpb.GetCarRequest{Id: carId.String()})
	if err != nil {
		return fmt.Errorf("cannot get car %v", err)
	}

	if car.Status != carpb.CarStatus_LOCKED {
		return fmt.Errorf("车辆状态错误 %v", err)
	}
	return nil
}

func (m *Manager) Unlock(cxt context.Context, carId id.CarID, aid id.AccountID, tid id.TripID, avatarURl string) error {
	_, err := m.CarService.UnlockCar(cxt, &carpb.UnlockCarRequest{
		Id: carId.String(),
		Driver: &carpb.Driver{
			Id:        aid.String(),
			AvatarUrl: avatarURl,
		},
		TripId: tid.String(),
	})

	if err != nil {
		return fmt.Errorf("开锁失败 %v", err)
	}
	return nil
}

func (m *Manager) Lock(cxt context.Context, carId id.CarID) error {
	_, err := m.CarService.LockCar(cxt, &carpb.LockCarRequest{Id: carId.String()})
	if err != nil {
		return fmt.Errorf("关锁失败 %v", err)
	}

	return nil
}
